package st.business.tepth.business.goodsreceive;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.ArrayList;
import java.util.List;

import st.business.tepth.business.bean.MaterialHadApplyBean;
import st.business.tepth.business.bean.MaterialWaitApplyBean;
import st.business.tepth.business.bean.PickGoods;
import st.business.tepth.business.bean.post.IDBean;
import st.business.tepth.business.goodsreceive.entity.GoodsReceiveLevel0;
import st.business.tepth.business.goodsreceive.entity.GoodsReceiveLevel1;
import st.business.tepth.config.ItemFields;
import st.shadow.library.ui.recycler.MultipleItemEntity;

import static st.business.tepth.business.goodsorder.DialogReturnGoods.FIELD_SELECT_COUNT;

/**
 * @author zhangyu
 * @date 2019/4/24
 */
public class GoodsReceiveConvert {
    public List<MultiItemEntity> convertToEntity(List<MaterialWaitApplyBean> list) {
        List<MultiItemEntity> entities = new ArrayList<>();
        if (list != null) {
            for (MaterialWaitApplyBean materialWaitApplyBean : list) {
                List<GoodsReceiveLevel1> subItems = new ArrayList<>();
                GoodsReceiveLevel0 level0 = new GoodsReceiveLevel0(materialWaitApplyBean, false);
                List<PickGoods> goodsBeanList = materialWaitApplyBean.getParts();
                for (int j = 0, size = goodsBeanList.size(); j < size; j++) {
                    subItems.add(new GoodsReceiveLevel1(goodsBeanList.get(j)));
                }
                level0.setSubItems(subItems);
                entities.add(level0);
            }
        }
        return entities;
    }

    public String convertToIDBeanString(MaterialHadApplyBean bean) {
        List<IDBean> idBeans = new ArrayList<>();
        if (bean != null) {
            List<PickGoods> pickGoods = bean.getParts();
            if (pickGoods != null) {
                for (PickGoods goods : pickGoods) {
                    idBeans.add(new IDBean(goods.getId(), goods.getValue()));
                }
            }
        }
        return JSON.toJSONString(idBeans);
    }

    public String convertToIDBeanString(List<MultipleItemEntity> entities) {
        List<IDBean> idBeans = new ArrayList<>();
        if (entities != null) {
            for (MultipleItemEntity entity : entities) {
                PickGoods pickGoods = entity.getField(ItemFields.ITEM_OBJ);
                idBeans.add(new IDBean(pickGoods.getId(),
                        (Float) entity.getField(FIELD_SELECT_COUNT)));
            }
        }
        return JSON.toJSONString(idBeans);
    }

}
